import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, Resolve } from '@angular/router';
import { ListResult } from 'app/modules/base/api-base';

import { Task, TaskService, TaskCategory, TaskListOption, TaskSince } from './task.service';

@Injectable({
	providedIn: 'root',
})
export class ListService implements Resolve<ListResult<Task>> {
	constructor(private taskService: TaskService) { }
	async resolve(route: ActivatedRouteSnapshot) {
		const page = Number(route.queryParamMap.get('page')) || 1;
		const count = Number(route.queryParamMap.get('count')) || 10;
		const category = route.url[0].path;
		let opt = { page, category, count } as TaskListOption;

		if (route.queryParamMap.has('type')) {
			opt.type = Number(route.queryParamMap.get('type'));
		}

		if (route.queryParamMap.has('period_type')) {
			opt.period_type = Number(route.queryParamMap.get('period_type'));
		}

		if (route.queryParamMap.has('status')) {
			opt.status = [Number(route.queryParamMap.get('status'))];
		}

		if (route.queryParamMap.has('since')) {
			opt.since = route.queryParamMap.get('since') as TaskSince;
		}

		if (route.queryParamMap.has('area_id')) {
			opt.area_id = Number(route.queryParamMap.get('area_id'));
		}

		return this.taskService.list(opt);
	}
}
